package week05.jdbc;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.sqlite.SQLiteDataSource;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.util.Properties;

/**
 * @author edward
 * @since 2021-07-25
 */
public class DataSourceFactory {
    public static DataSource getDataSource(String type) {
        if ("sqlite".equalsIgnoreCase(type)) {
            SQLiteDataSource ds = new SQLiteDataSource();
            ds.setUrl(ds.getUrl() + "test.db");
            return ds;
        } else if ("hikari".equalsIgnoreCase(type)) {
            Properties props = new Properties();
            props.setProperty("dataSourceClassName", "org.sqlite.SQLiteDataSource");
            props.setProperty("dataSource.databaseName", "hikaritest");
            props.put("dataSource.logWriter", new PrintWriter(System.out));

            HikariConfig config = new HikariConfig(props);
            return new HikariDataSource(config);
        }
        throw new UnsupportedOperationException("Not support type=" + type);
    }
}
